﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Transportr
{
    public static class FavoriteStore
    {
        private static string filepath;
        public static bool Valid { get; private set; }
        public static event UpdatedHandler Update;

        public class LineDetails
        {
            [XmlAttribute]
            public int NumberOfUses;
            [XmlElement]
            public ServiceModel.Line Brief;
            [XmlElement]
            public ServiceModel.LineInfo Stations;
            [XmlElement]
            public ServiceModel.Terms Terms;
        }

        public class Segment
        {
            [XmlAttribute]
            public int stIdBegin;
            [XmlAttribute]
            public int stIdEnd;
            [XmlAttribute]
            public int lnId;
            [XmlAttribute]
            public string lnName;
        }

        public class Route
        {
            [XmlAttribute]
            public int Id;
            [XmlAttribute]
            public string Name;
            [XmlArray]
            public List<ServiceModel.Station> Stations;
            [XmlElement]
            public List<Segment> Segments;

            public Route()
            {
                Stations = new List<ServiceModel.Station>();
                Segments = new List<Segment>();
            }
        }

        [XmlRoot]
        public class Favorites
        {
            [XmlElement]
            public List<LineDetails> Lines;
            [XmlElement]
            public List<Route> Routes;
            [XmlElement]
            public List<Map.GeoArea> Areas;

            public Favorites()
            {
                Lines = new List<LineDetails>();
                Routes = new List<Route>();
                Areas = new List<Map.GeoArea>();
            }
        }

        public static Favorites Store { get; private set; }

        public static void Initialize()
        {
            filepath = "favorites.xml";
            Store = new Favorites();
            Load();
            if (Update != null) Update("");
        }

        public static void Load()
        {
            if (FileStorage.FileExists(filepath))
            {
                try
                {
                    using (FileStream fs = FileStorage.GetFileStream(filepath, FileMode.Open))
                    {
                        Store = (Favorites)(new XmlSerializer(typeof(Favorites)).Deserialize(fs));
                    }
                    Valid = true;
                }
                catch (InvalidOperationException x)
                {
                    Valid = false;
                    System.Diagnostics.Debug.WriteLine(x.Message);
                }
            }
            else
            {
                Valid = false;
            }
        }

        public static void Save()
        {
            try
            {
                using (FileStream fs = FileStorage.GetFileStream(filepath, FileMode.Create))
                {
                    new XmlSerializer(typeof(Favorites)).Serialize(fs, Store);
                }
                if (Update != null) Update("");
            }
            catch (Exception x)
            {
                System.Diagnostics.Debug.WriteLine(x.Message);
            }
        }
    }
}
